package org.gdstash.ui.select;

import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.DocumentFilter;
import java.awt.*;

public class StatSelPane extends AdjustablePanel {
    private JLabel lblPhysiqueMax;
    private JLabel lblCunningMax;
    private JLabel lblSpiritMax;
    private JFormattedTextField ftPhysiqueMax;
    private JFormattedTextField ftCunningMax;
    private JFormattedTextField ftSpiritMax;

    public StatSelPane(final int direction) {
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        if (direction == 0) {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblPhysiqueMax)).addGroup(layout.createParallelGroup().addComponent(this.ftPhysiqueMax)).addGroup(layout.createParallelGroup().addComponent(this.lblCunningMax)).addGroup(layout.createParallelGroup().addComponent(this.ftCunningMax)).addGroup(layout.createParallelGroup().addComponent(this.lblSpiritMax)).addGroup(layout.createParallelGroup().addComponent(this.ftSpiritMax));
        } else {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblPhysiqueMax).addComponent(this.lblCunningMax).addComponent(this.lblSpiritMax)).addGroup(layout.createParallelGroup().addComponent(this.ftPhysiqueMax).addComponent(this.ftCunningMax).addComponent(this.ftSpiritMax));
        }
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        if (direction == 0) {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblPhysiqueMax)).addGroup(layout.createParallelGroup().addComponent(this.ftPhysiqueMax)).addGroup(layout.createParallelGroup().addComponent(this.lblCunningMax)).addGroup(layout.createParallelGroup().addComponent(this.ftCunningMax)).addGroup(layout.createParallelGroup().addComponent(this.lblSpiritMax)).addGroup(layout.createParallelGroup().addComponent(this.ftSpiritMax));
        } else {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblPhysiqueMax).addComponent(this.ftPhysiqueMax)).addGroup(layout.createParallelGroup().addComponent(this.lblCunningMax).addComponent(this.ftCunningMax)).addGroup(layout.createParallelGroup().addComponent(this.lblSpiritMax).addComponent(this.ftSpiritMax));
        }
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblPhysiqueMax, this.ftPhysiqueMax);
        layout.linkSize(0, this.lblPhysiqueMax, this.ftCunningMax);
        layout.linkSize(0, this.lblPhysiqueMax, this.lblCunningMax);
        layout.linkSize(0, this.lblPhysiqueMax, this.lblSpiritMax);
        layout.linkSize(0, this.lblPhysiqueMax, this.ftSpiritMax);
        layout.linkSize(1, this.lblPhysiqueMax, this.ftPhysiqueMax);
        layout.linkSize(1, this.lblPhysiqueMax, this.ftCunningMax);
        layout.linkSize(1, this.lblPhysiqueMax, this.lblCunningMax);
        layout.linkSize(1, this.lblPhysiqueMax, this.lblSpiritMax);
        layout.linkSize(1, this.lblPhysiqueMax, this.ftSpiritMax);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntFText = UIManager.getDefaults().getFont("FormattedTextField.font");
        if (fntFText == null) {
            fntFText = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntFText = fntFText.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        if (this.lblPhysiqueMax == null) {
            this.lblPhysiqueMax = new JLabel();
        }
        this.lblPhysiqueMax.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_PHYSIQUE_MAX"));
        this.lblPhysiqueMax.setFont(fntLabel);
        if (this.ftPhysiqueMax == null) {
            this.ftPhysiqueMax = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftPhysiqueMax.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftPhysiqueMax.setFont(fntFText);
        if (this.lblCunningMax == null) {
            this.lblCunningMax = new JLabel();
        }
        this.lblCunningMax.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_CUNNING_MAX"));
        this.lblCunningMax.setFont(fntLabel);
        if (this.ftCunningMax == null) {
            this.ftCunningMax = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftCunningMax.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftCunningMax.setFont(fntFText);
        if (this.lblSpiritMax == null) {
            this.lblSpiritMax = new JLabel();
        }
        this.lblSpiritMax.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SPIRIT_MAX"));
        this.lblSpiritMax.setFont(fntLabel);
        if (this.ftSpiritMax == null) {
            this.ftSpiritMax = new JFormattedTextField();
            final DocumentFilter filter = new IntLenDocFilter(3);
            final AbstractDocument doc = (AbstractDocument) this.ftSpiritMax.getDocument();
            doc.setDocumentFilter(filter);
        }
        this.ftSpiritMax.setFont(fntFText);
    }
}
